#include "explosion.h"

ofImage explosion::explosive;
explosion::explosion(ofxVec2f pos, float mass)
{
	this->mass = mass;

	dead = false;
	position = pos;
    seed1 = ofRandom(1.0f,2.0f);
    seed2 = ofRandom(1.0f,2.0f);
	timeToLive = ofGetElapsedTimef()+settings::TIME_TO_LIVE_WITH_EXPLOSION;
	timeToFade = ofGetElapsedTimef()+settings::TIME_TO_FADE_EXPLOSION;
	readyToDie = true;
	currentSize = 1;
	maxSize = mass*3;
	this->mass = 10;
	timeToGrow = ofGetElapsedTimef()+settings::TIME_TO_GROW_EXPLOSION;
	explosionTime = ofGetElapsedTimef();
	dieBeforeSessionEnd = true;
	explosive.loadImage("data/explosive.png");
}


explosion::~explosion(void)
{
}



int explosion::update()
{
	if(timeToLive<ofGetElapsedTimef() && readyToDie)
		dead = true;

	if(timeToGrow>ofGetElapsedTimef())
	{//grow
		currentSize = (1-(timeToGrow-ofGetElapsedTimef())/settings::TIME_TO_GROW_EXPLOSION)*maxSize;
		timeToShrink = ofGetElapsedTimef()+settings::TIME_TO_SHRINK_EXPLOSION;
		explosionAlpha =255;
	}
	else if(timeToShrink>ofGetElapsedTimef())
	{
		//currentSize = maxSize-(1-(timeToShrink-ofGetElapsedTimef())/TIME_TO_SHRINK_EXPLOSION)*(maxSize-mass);
		explosionAlpha =((timeToShrink-ofGetElapsedTimef())/settings::TIME_TO_SHRINK_EXPLOSION)*255;

	}
	/*else
		currentSize = mass;*/


	return 1;
}

void explosion::draw()
{
	if(dead)
		return;


	float imageSize = mass*6;
	float imageCurrentSize = currentSize*1.9f;
	int alpha = 255;

	if(timeToFade< ofGetElapsedTimef() && readyToDie)
	{

		alpha = ((timeToLive-ofGetElapsedTimef()) / (settings::TIME_TO_LIVE_WITH_EXPLOSION-settings::TIME_TO_FADE_EXPLOSION))*255;
	}

	ofSetColor(150,150,150,explosionAlpha);
	//ofCircle(position.x,position.y,currentSize);

	explosive.draw(position.x-imageCurrentSize,position.y-imageCurrentSize,imageCurrentSize*2,imageCurrentSize*2);
	explosive.draw(position.x-imageCurrentSize,position.y-imageCurrentSize,imageCurrentSize*2,imageCurrentSize*2);


	ofSetColor(255,255,255,alpha);
	//ofCircle(position.x,position.y,mass);

    float baseDist = 50;
    float speed = 0.08f;
    ofSetColor(255,255,255,alpha);
	explosive.draw(position.x-imageSize,position.y-imageSize,imageSize*3,imageSize*3);
	ofSetColor(255,255,255,alpha);
    for(int i = 0; i<6;i++)
    {
        float dist = baseDist+i/6.0f;
        float size = imageSize*(3+(i/6));
        explosive.draw(position.x-size/2+sinf(speed*ofGetElapsedTimef()*(2+seed1*2)*seed1+i*seed2)*dist,position.y-size/2+sinf(speed*ofGetElapsedTimef()*(2+seed2*2)*seed2+i*seed1)*dist,size,size);


    }
}

void explosion::die()
{
	if(readyToDie)
		return;
	readyToDie = true;
	timeToLive = ofGetElapsedTimef()+settings::TIME_TO_LIVE_WITH_EXPLOSION;
	timeToFade = ofGetElapsedTimef()+settings::TIME_TO_FADE_EXPLOSION;
}
